﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using DAO;
using BUS;
using System.Globalization;

namespace SSN.MasterPages
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (IsPostBack)
            {
                #region Lay Gia Tri Duoc Submit
                string ho = Request.QueryString["lastname"];
                string ten = Request.QueryString["firstname"];
                string username = Request.QueryString["username"];
                string email = Request.QueryString["email"];
                string reemail = Request.QueryString["reemail"];
                string password = Request.QueryString["password"];
                string repassword = Request.QueryString["repassword"];
                string gender = Request.QueryString["gender"];
                string day = Request.QueryString["day"];
                string month = Request.QueryString["month"];
                string year = Request.QueryString["year"];
                DateTime dob = ChuyenThanhNgay(day, month, year);
                #endregion

                #region Kiem tra dang ky
                if (KiemTraTenDangNhap(username) == 1 && KiemTraMatKhau(password, repassword) == 1 && KiemTraEmail(email, reemail) == 1)
                {
                    NguoiDung nd = new NguoiDung();
                    nd.Ho = ho;
                    nd.Ten = ten;
                    nd.TenDangNhap = username;
                    nd.MatKhau = HashPassword(password);
                    nd.Email = email;
                    nd.GioiTinh = KiemTraGioiTinh(gender);
                    nd.NgaySinh = dob;
                    nd.EmailXacNhan = email;

                    NguoiDungBUS nguoidungBUS = new NguoiDungBUS();
                    nguoidungBUS.ThemNguoiDung(nd);

                    Response.Redirect("~/Loggedin/Home.aspx");
                }
                else
                {
                    if (KiemTraHopLe(username) == 1)
                    {
                        if (KiemTraTonTaiTenDangNhap(username) == 1)
                            lblError_TenDangNhap.Text = "Tên đăng nhập đã được chọn";
                    }
                    else
                        lblError_TenDangNhap.Text = "Tên đăng nhập không hợp lệ";

                    if (KiemTraEmail(email, reemail) == 0)
                        lblError_Email.Text = "Email không hợp lệ";
                    else
                        if (KiemTraEmail(email, reemail) == -1)
                            lblError_Email.Text = "Bạn xác nhận email chưa chính xác";
                        else
                            if (KiemTraEmail(email, reemail) == -2)
                                lblError_Email.Text = "Email này đã được sử dụng";

                    if (KiemTraMatKhau(password, repassword) == -1)
                        lblError_MatKhau.Text = "Xác nhận mật khẩu không đúng";
                    else
                        if(KiemTraMatKhau(password, repassword) == 0)
                            lblError_MatKhau.Text = "Mật khẩu phải từ 6 kí tự trở lên";
                        
                    
                    
                    
                }
                #endregion
            }
        }

        private string HashPassword(string pwd)
        {
            HashAlgorithm hashAlg = new SHA256CryptoServiceProvider();
            byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(pwd);
            byte[] bytHash = hashAlg.ComputeHash(bytValue);
            string hashedpwd = Convert.ToBase64String(bytHash);
            return hashedpwd;
        }

        private int KiemTraTonTaiTenDangNhap(string tennd)
        {
            NguoiDungBUS nguoidungBUS = new NguoiDungBUS();
            NguoiDung ketqua = nguoidungBUS.LietKe().SingleOrDefault(n => n.TenDangNhap == tennd);
            if (ketqua != null)
                return 1; //Da ton tai tai khoan
            return 0; //Chua ton tai
        }

        private int KiemTraTonTaiEmail(string email)
        {
            NguoiDungBUS nguoidungBUS = new NguoiDungBUS();
            NguoiDung ketqua = nguoidungBUS.LietKe().SingleOrDefault(n => n.Email == email);
            if (ketqua != null)
                return 1; //Da ton tai tai khoan
            return 0; //Chua ton tai
        }

        private int KiemTraHopLe(string tennd)
        {
            string[] kytudacbiet = { "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "/", "+", "=" };
            foreach (string item in kytudacbiet)
            {
                if (tennd.Contains(item))
                {
                    return 0;
                }
            }
            return 1;
        }

        private int KiemTraTenDangNhap(string tennd)
        {
            if (KiemTraHopLe(tennd) == 1)
            {
                if (KiemTraTonTaiTenDangNhap(tennd) == 0)
                    return 1; //Co the tao tai khoan
                return 0; //Da co tai khoan ton tai
            }
            return -1; //Ten tai khoan khong hop le
        }

        private int KiemTraMatKhau(string mk1, string mk2)
        {
            if (mk1.Length > 7)
            {
                if (mk1.CompareTo(mk2) == 0)
                    return 1; //Mat khau hop le
                return -1; //Xac nhan mat khau khong chinh xac
            }
            return 0; //Mat khau khong hop le, khong du do dai
        }

        private int KiemTraEmail(string email1, string email2)
        {
            //Kiem tra Email co hop le hay khong (phai co @, .com)
            if (email1.Contains('@'))
            {
                if (email1.CompareTo(email2) == 0)
                {
                    if (KiemTraTonTaiEmail(email1) == 0)
                        return 1; //Email hop le
                    else
                        return -2; //Email da ton tai
                }
                else
                    return -1; //Xac nhan Email khong chinh xac
            }
            return 0; //Email khong hop le
        }

        private int KiemTraGioiTinh(string gioitinh)
        {
            if (gioitinh.CompareTo("1") == 0)
            {
                return 1; //La nam
            }
            if (gioitinh.CompareTo("0") == 0)
            {
                return 0; //La nu
            }
            return -1; //Chua chon gioi tinh
        }

        private DateTime ChuyenThanhNgay(string ngay, string thang, string nam)
        {
            string strDate = "";
            strDate = strDate + ngay + "-" + thang + "-" + nam;
            DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
            dtfi.ShortDatePattern = "dd-MM-yyyy";
            dtfi.DateSeparator = "-";
            DateTime objDate = Convert.ToDateTime(strDate, dtfi);
            return objDate;
        }
    }
}